﻿using PSI.Filters;
using PSI.Helper;
using PSI.Models;
using PSI.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace PSI.Controllers {

    public class HomeController : PSIController {
        private UserService UserService { get { return new UserService(); } }

        public ActionResult MainPage()
        {
            ViewBag.UserName = UserService.GetLoginUserName();


            if (PSI.Services.UserService.IsLogin)
            {
                if (PSI.Services.UserService.GetLoginUser().Password == "123456")
                {
                    return Redirect("/User/ChangeMyPassword?type=system");
                }
                else
                {
                    return View();
                }
            }
            else
            {
                return View();
            }
        }
    
        
        public ActionResult Index()
        {
           
            return View();
        }

        [HttpPost]
        public ActionResult GetMainMenuItems() {
            var root = new MainMenuService().BuildMenuItemTree(false);

            return Json(root);
        }

        public ActionResult NavigateTo(string fid) {
            new FIdService().InsertRecentFId(fid);

            new LogService().Log("进入模块:" + new MainMenuService().GetFidCaption(fid));

            switch (fid) {
                case FIdConst.RE_LOGIN:
                    // 重新登录
                    new LogService().Log("退出系统(重新登录功能)");
                    UserService.ClearLoginUser();
                    return Redirect("/");
                case FIdConst.CHANGE_MY_PASSWORD:
                    // 修改我的密码
                    return Redirect("/User/ChangeMyPassword?type=myself");
         
                case FIdConst.USER:
                    // 用户管理
                    return Redirect("/User/Index");
                case FIdConst.completeReport:
                    //计划完成汇总报表
                    return Redirect("/Report/Index");
                case FIdConst.ImportplanReport:
                    //进口计划汇总表
                    return Redirect("/InPlan/ImprotPlanReport");

                case FIdConst.ExportplanReport:
                    //出口计划汇总表
                    return Redirect("/OutPlan/ExportPlanReport");
                case FIdConst.NewCompleteReport:
                    //出口计划汇总表
                    return Redirect("/InPlan/CompositeReport");
                case FIdConst.PERMISSION:
                    // 权限管理
                    return Redirect("/Permission/Index");
                case FIdConst.LOG:
                    // 系统日志
                    return Redirect("/Log/Index");
                case FIdConst.Knowledge:
                    // 知识管理
                    return Redirect("/Knowledge/Index");
                case FIdConst.Massage:
                    //通知
                    return Redirect("/Massage/Index");
                case FIdConst.Knowledge_backs:
                    // 知识管理
                    return Redirect("/Knowledge/IndexBacks");
                case FIdConst.IN_CONSIGNATION:
                    // 进口委托
                    return Redirect("/InConsignation/Index");
                case FIdConst.IN_PLAN:
                    // 进口计划
                    return Redirect("/InPlan/Index");
                case FIdConst.OUT_CONSIGNATION:
                    // 出口委托
                    return Redirect("/OutConsignation/Index");
                case FIdConst.OUT_PLAN:
                    // 出口计划
                    return Redirect("/OutPlan/Index");
                case FIdConst.TRUCK_MANAGEMENT:
                    // 车辆管理
                    return Redirect("/Truck/Index");
                case FIdConst.IN_PLAN_MAKE_PLAN:
                    // 出口计划-下达计划
                    return Redirect("/InPlan/MakePlanIndex");
                case FIdConst.IN_PLAN_SEND_TRUCK:
                    // 进口计划-派车
                    return Redirect("/InPlan/SendTruckIndex");
                case FIdConst.IN_PLAN_TRACE:
                    // 进口计划-动态跟踪
                    return Redirect("/InPlan/TraceIndex");
                case FIdConst.IN_PLAN_Evaluate:
                    // 进口计划-评价服务
                    return Redirect("/InPlan/EvaluateService");
                case FIdConst.IN_PLAN_RESERATION:
                    // 进口计划-进口审核
                    return Redirect("/InConsignation/ReserationImportView");
                case FIdConst.OUT_PLAN_MAKE_PLAN:
                    // 出口计划-下达计划
                    return Redirect("/OutPlan/MakePlanIndex");
                case FIdConst.OUT_PLAN_EvaluateService:
                    // 出口计划-服务评价
                    return Redirect("/OutPlan/EvaluateService");
                case FIdConst.OUT_PLAN_SEND_TRUCK:
                    // 出口计划-派车
                    return Redirect("/OutPlan/SendTruckIndex");
                case FIdConst.OUT_PLAN_TRACE:
                    // 出口计划-动态跟踪
                    return Redirect("/OutPlan/TraceIndex");
                case FIdConst.OUT_RES:
                    // 出口计划-出口审核
                    return Redirect("/OutConsignation/ExportResView");
                case FIdConst.DISPATDHER_DATA_LIST:
                    // 调度台账
                    return Redirect("/Dispatcher/Index");
                case FIdConst.ROUTE:
                    // 线路
                    return Redirect("/Route/Index");
                case FIdConst.FUEL:
                    // 油耗
                    return Redirect("/Fuel/Index");
                case FIdConst.CNT_TYPE:
                    // 箱型
                    return Redirect("/CntType/Index");
                case FIdConst.VSL:
                    // 船名
                    return Redirect("/Vsl/Index");
                case FIdConst.CLIENTS:
                    // 委托方
                    return Redirect("/Clients/Index");
                case FIdConst.CNT_OWNER:
                    // 箱属
                    return Redirect("/CntOwner/Index");
                case FIdConst.ADDRESS:
                    // 门点
                    return Redirect("/Address/Index");
                case FIdConst.CUSTOMER:
                    // 货主
                    return Redirect("/Customer/Index");
                case FIdConst.BIZ_MAN:
                    // 业务员
                    return Redirect("/BizMan/Index");
                case FIdConst.BC_DISPATCHER:
                    // 调度，基础数据
                    return Redirect("/BcDispatcher/Index");
                case FIdConst.PORT:
                    // 港口
                    return Redirect("/Port/Index");
                case FIdConst.RECEIVING_FEE:
                    // 应收费目
                    return Redirect("/ReceivingFee/Index");
                case FIdConst.PAYABLE_FEE:
                    // 应付费目
                    return Redirect("/PayableFee/Index");
                case FIdConst.SRV_COMMENTS_REPORT:
                    // 服务评价，报表
                    return Redirect("/SrvCommentsReport/Index");
                default:
                    return View();
            }
        }

        public ActionResult GetImage(string id) {
            var fileName = Server.MapPath("~/Images/fid/fid" + id + ".png");
            if (!System.IO.File.Exists(fileName)) {
                fileName = Server.MapPath("~/Images/fid/default.png");
            }

            return File(fileName, "image/png");
        }

        [HttpPost]
        public ActionResult GetRecentFid() {
            var data = new FIdService().GetRecentFid();

            return Json(data);
        }
    }
}
